// 故障源统计-按年统计Tab页js文件

function initFaultCountChartDeptYear() {
	initDataTable();
	var d = new Date();
	var currentYear = d.getFullYear();
	$("#countYearDept").val(currentYear);
	$('#countYearDept').datetimepicker({
		startView : 4,
		minView : 4,
		autoclose : true,
		format : 'yyyy'
	});
	changeDeptCountYearTable();
}

function changeDeptCountYearTable() {
	var year = $("#countYearDept").val();
	changeDeptCountYear([ {
		name : 'year',
		value : year
	} ]);
}

function initFaultCountByYearChartData() {
	var year = $("#countYearDept").val();
	$.getJSON("/rest/faultCount/getYearCountInfos/" + year, function(result) {
		initData(result);
	});
}

function initData(data) {
	var indexV = document.getElementById("yearCountForm:ds").selectedIndex; // 获取当前选择项的索引
	var itemValue = document.getElementById("yearCountForm:ds").options[indexV].value; // 获取当前选择项的值
	var dataSource;
	if (itemValue == "0") {
		dataSource = "集中录波";
	} else if (itemValue == "1") {
		dataSource = "保护录波";
	} else if (itemValue == "2") {
		dataSource = "保护事件";
	}
	var year = $("#countYearDept").val();
	var desorgTitle = year + "年各调度单位故障" + dataSource + "上送情况";
	var subcompanyTitle = year + "年各运维单位故障" + dataSource + "上送情况";
	$("#subcomChartTitle").text(subcompanyTitle);
	$("#desorgChartTitle").text(desorgTitle);

	var desorgChartData = data[0];
	var desResult = [];
	for (var i = 0; i < desorgChartData.length; i++) {
		if (desorgChartData[i].dataSource == dataSource) {
			desResult.push(desorgChartData[i]);
		}
	}
	initFaultsCountByYearChart("desorgChart", desResult);

	var subResult = [];
	var subcomChartData = data[1];
	for (var i = 0; i < subcomChartData.length; i++) {
		if (subcomChartData[i].dataSource == dataSource) {
			subResult.push(subcomChartData[i]);
		}
	}
	initFaultsCountByYearChart("subcomChart", subResult);
}

function initFaultsCountByYearChart(chartId, chartData) {
	var desorgChart = new CategoryLine(chartId);
	var xValues = [];
	for (var i = 1; i < 13; i++) {
		xValues.push(i + "月");
	}
	desorgChart.setxAxis(xValues);

	var yAxisName = [];
	var yValues = [];
	for (var j = 0; j < chartData.length; j++) {
		yAxisName.push(chartData[j].comp);
		var oneMonthValue = [];
		var ratio = chartData[j].value;
		for (var i = 0; i < 12; i++) {
			oneMonthValue.push(ratio[i][3]);
		}
		yValues.push(oneMonthValue);
	}

	for (var i = 0; i < 12; i++) {
		desorgChart.setyAxis(yValues[i]);
	}

	desorgChart.setxAxisTitle("月份");
	desorgChart.setyAxisTitle("上送率");
	var opt = desorgChart.getOption();
	for (var i = 0; i < 12; i++) {
		opt.series[i].label.normal.position = "top";
		opt.series[i].name = yAxisName[i];
		desorgChart.setyAxisName(yAxisName[i]);
	}
	desorgChart.show();
}